# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=scite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-defaults")
pkgver=4.3.3
pkgrel=1
arch=('any')
url='https://www.scintilla.org/SciTE.html'
license=('custom:scite')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("https://www.scintilla.org/${_realname}${pkgver//./}.tgz"
        "0001-Use-POSIX-tools-in-makefiles.patch"
        "0002-Prefix-library-names.patch"
        "0003-Use-FHS.patch")
sha256sums=('134b5e90d1e9adb7fdacbf66821252e690a828805be6a7e2746c4917198b346f'
            'df23e0fdc65a50645b274a85464d387a2e3f78cd6f6396b2c53978b1cb7f42b7'
            'ba2a58eee2986cd2c3e9b25a9f62b530a087b352fbc403ffcd283972e95dd135'
            'ad3f4fa6e6e2ac9e20fbb3961682a9009788134081075ae9737f0b68a4284c30')

prepare() {
  cd "${srcdir}"
  patch -p1 -i "${srcdir}"/0001-Use-POSIX-tools-in-makefiles.patch
  patch -p1 -i "${srcdir}"/0002-Prefix-library-names.patch
  patch -p1 -i "${srcdir}"/0003-Use-FHS.patch
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}
  cp -r "${srcdir}"/{scite,scintilla} "${srcdir}"/build-${MINGW_CHOST}
  cd "${srcdir}"/build-${MINGW_CHOST}

  GTK3=1 CC="${MINGW_PREFIX}/bin/gcc" CXX="${MINGW_PREFIX}/bin/g++" make -C scintilla/gtk
  CC="${MINGW_PREFIX}/bin/gcc" CXX="${MINGW_PREFIX}/bin/g++" make -C scintilla/win32
  # GTK3=1 make -C scite/gtk
  CC="${MINGW_PREFIX}/bin/gcc" CXX="${MINGW_PREFIX}/bin/g++" make -C scite/win32
}

package_mingw-w64-scite() {
  pkgdesc="Editor with facilities for building and running programs (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-gtk3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}-defaults: Default language files")

  cd "${srcdir}"/build-${MINGW_CHOST}

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/bin
  cp scintilla/bin/lib{Scintilla,SciLexer}.dll "${pkgdir}"${MINGW_PREFIX}/bin
  cp scite/bin/SciTE.exe "${pkgdir}"${MINGW_PREFIX}/bin

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/lib
  cp scintilla/bin/libScintillaGtk.a "${pkgdir}"${MINGW_PREFIX}/lib

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/include/scintilla
  cp scintilla/include/{*.h,*.iface} "${pkgdir}"${MINGW_PREFIX}/include/scintilla

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/scite
  cp scite/bin/SciTEGlobal.properties "${pkgdir}"${MINGW_PREFIX}/share/scite

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/doc/scite
  cp scite/doc/{*.png,*.jpg,*.html} "${pkgdir}"${MINGW_PREFIX}/share/doc/scite

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/man/man1
  cp scite/doc/scite.1 "${pkgdir}"${MINGW_PREFIX}/share/man/man1/SciTE.1

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite
  cp scintilla/License.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-scintilla
  cp scite/License.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-scite
  cp scite/lua/COPYRIGHT "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-lua
}

package_mingw-w64-scite-defaults() {
  pkgdesc="Default language files for the SciTE editor (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")

  cd "${srcdir}"/build-${MINGW_CHOST}

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/scite
  cp scite/bin/*.properties "${pkgdir}"${MINGW_PREFIX}/share/scite
  rm -f "${pkgdir}"${MINGW_PREFIX}/share/scite/SciTEGlobal.properties
}

package_mingw-w64-i686-scite() {
  package_mingw-w64-scite
}

package_mingw-w64-i686-scite-defaults() {
  package_mingw-w64-scite-defaults
}

package_mingw-w64-x86_64-scite() {
  package_mingw-w64-scite
}

package_mingw-w64-x86_64-scite-defaults() {
  package_mingw-w64-scite-defaults
}
